<?php

class PapersController extends Controller {

    public function actionIndex() {
        $this->render('index');
    }

    public function actionPushAll() {
        $status = Constants::STATUS_OK;
        $info = "";
        $code = "0"; //0表示注册成功
        $mesg = "成功,感谢使用推送。";
        $getuiHelper = new GetuiPushHelper;
        if (isset($_GET['uid']) &&
                isset($_GET['pid'])) {
            $uid = $_GET['uid'];
            $pid = $_GET['pid'];
            //	$pushCidModel->saveInfo($uid, $push_id);
            $getuiHelper->pushMessageToApp();

            $info = array("code" => $code, "mesg" => $mesg);
        } else {
            $status = Constants::STATUS_ERROR;
            $info = "need input uid and pid";
        }
        $ret = AppInterfaceHelper::createArrayResult($status, $info);
        echo "<br>";
        echo json_encode($ret);
        Yii::app()->end();
    }

    /*
     * @function: 基于用户的订阅行为。$uid订阅了$indcator
     */

    public function actionSubscribe($uid, $iid) {
        $status = Constants::STATUS_OK;
        $info = "";
        if (isset($_REQUEST['uid']) && isset($_REQUEST['iid'])) {
            $uid = $_REQUEST['uid'];
            $iid = $_REQUEST['iid'];
            $model = Subscribe::model();
            $ret = $model->doInsert($uid, $iid);
            if ($ret == 0) {
                $info = "successfully subsribe";
            } else {
                $status = Constants::STATUS_ERROR;
                $info = "error while subscribe";
            }
        } else {
            $status = Constants::STATUS_ERROR;
            $info = "need input uid and iid(indicator id)";
        }
        $ret = AppInterfaceHelper::createArrayResult($status, $info);
        echo json_encode($ret);
        Yii::app()->end();
    }

    public function actionGetPaperList() {
        $status = Constants::STATUS_OK;
        $info = "";
        if (isset($_GET['uid'])) {
            $model = Papers::model();
            $uid = $_GET['uid'];
            $start = isset($_GET['start']) ? $_GET['start'] : 0;
            $offset = isset($_GET['offset']) ? $_GET['offset'] : 0;
            //获取数据,先不处理start, offset
            $data = $model->getAllTypePaperList($uid);
            $info = $data;
        } else {
            $status = Constants::STATUS_ERROR;
            $info = "need input uid and iid(indicator id)";
        }
        $ret = AppInterfaceHelper::createArrayResult($status, $info);
        echo json_encode($ret);
        Yii::app()->end();
    }

    /**
     * 获取首页3条资讯
     * 
     * @param int $cn
     *        	体检中心ID，默认为0，通用版
     */
    public function actionGetHomeNews() {
        $news = InfoHomeNews::model()->getHomeNews();
        if($news===false){
            JSONHelper::echoJSON(FALSE,  JSONHelper::ERROR_SERVICE);
        }
        JSONHelper::echoJSON(true, $news);
        // var_dump($url);break;
    }

    /**
     * 显示首页文章内容详情
     * 
     * @param int $id
     *        	文章ID
     */
    public function actionView() {
        header("Content-type:text/html;charset=utf-8");
        if (!isset($_GET ["id"])) {
            throw new CHttpException(200, '非法的请求');
        }
        $ret = InfoHomeNews::model()->findByPk($_GET ["id"]);
        if (null === $ret) {
            throw new CHttpException(200, '找不到文章');
        }
        $this->renderPartial("view", array(
            "item" => $ret
        ));
    }

}
